package org.bt.common.validator;

import org.bt.common.validator.aspect.FixedValue;
import org.bt.common.validator.aspect.FixedValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * FixedValueValidator
 * <p>
 * Created on 2020/1/9
 *
 * @author tongw
 */
public class FixedValueValidator implements ConstraintValidator<FixedValue, String> {

    private boolean require = true;
    private String[] values = null;

    @Override
    public void initialize(FixedValue constraintAnnotation) {
        this.values = constraintAnnotation.values();
        this.require = constraintAnnotation.required();

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null || values == null) {
            return true;
        }

        if (require) {
            return ValidatorUtils.isFixedValue(value, values);
        } else {
            return true;
        }
    }
}
